import { RequestConfig } from 'umi';
import { getToken } from '@/utils/index';
// import {createLogger} from 'redux-logger'
import { message } from 'antd';

const baseURL = '//10.34.6.4:7002';
// const baseURL = '//10.34.6.3:7002';
export const request: RequestConfig = {
  errorConfig: {
    adaptor: (resData) => {
      // console.log('resData...', resData);
      return {
        success: true,
      };
    },
  },
  requestInterceptors: [
    (url: any, options: any) => {
      url = baseURL + url;
      // console.log('url', url);
      let headers = {};
      if (getToken()) {
        headers = { authorization: getToken() };
      }
      return {
        url,
        options: { ...options, headers },
      };
    },
  ],
  responseInterceptors: [
    (response: any) => {
      const codeMaps: { [key: string]: string } = {
        400: '错误请求',
        403: '请求禁止访问',
        404: '请求资源不存在',
        500: '服务器内部错误',
        502: '网关错误',
        503: '服务不可用，服务器暂时过载或维护',
        504: '网关超时',
      };
      // console.log('response...', response);
      if (response.status !== 200) {
        message.error(codeMaps[response.status]);
      }
      return response;
    },
  ],
};

// dva配置
// export const dva = {
//   config: {
//     onAction: createLogger()
//   },
// };
